package com.lxy.controller.Common;

import com.alibaba.fastjson.JSON;
import com.lxy.common.R;
import com.lxy.utils.AliOssUtil;
import com.lxy.utils.ValidateCodeUtils;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.Date;
import java.util.Enumeration;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

@Slf4j
@RestController
@RequestMapping("/common")
public class CommonController {
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private JavaMailSenderImpl mailSender;
    @Autowired
    private AliOssUtil aliOssUtil;

    /**
     * 发送验证码到邮箱
     * @param email
     * @return
     */
    @ApiOperation("发送验证码到邮箱")
    @GetMapping("/sendCode")
    public R<String> sendCode(String email,String type) throws Exception {
       try{
           String key=email+type;
           //检验是否存在这条缓存
           ValueOperations valueOperations =redisTemplate.opsForValue();
           Object o = valueOperations.get(key);
           if(o!=null){
               redisTemplate.delete(key);
           }
           //生成六位验证码
           Integer code = ValidateCodeUtils.generateValidateCode(6);
           //将验证码存入Redis中
           valueOperations.setIfAbsent(key,code,3, TimeUnit.MINUTES);
           //发送邮箱
           SimpleMailMessage mailMessage = new SimpleMailMessage(); //创建一个邮件信息
           System.out.println("当前时间" + new Date());
           mailMessage.setSubject("您的验证码是");//邮件的主题 TODO
           mailMessage.setText("您的验证码是："+code);//邮件的内容
           mailMessage.setTo(email);//接收者的邮箱
           mailMessage.setFrom("3319725459@qq.com");//发送者的邮箱
           mailSender.send(mailMessage);//发送邮件
           //方便操作，记得删除 TODO
           System.out.println(code);
           return R.success("验证码发送成功");
       }catch (Exception exception) {
            throw new Exception("验证码发送错误，请检查邮箱");
       }
    }

    @ApiOperation("文件上传")
    @PostMapping("/upload")
    //MultipartFile 定义的实体类名称需要和前端传来的数据名称一致
    public R<String> upload(MultipartFile file){
        try {
            //获取原始文件名
            String originalFilename = file.getOriginalFilename();
            //获取后缀
            String extension = originalFilename.substring(originalFilename.lastIndexOf("."));
            //构造新的文件名
            String objectName= UUID.randomUUID().toString()+extension;
            //文件请求的路径
            String filePath = aliOssUtil.upload(file.getBytes(), objectName);
            return R.success(filePath);
        } catch (IOException e) {
            log.error("文件上传失败：{}",e);
        }
        return R.error("文件上传失败");
    }






}
